package com.example.demo.controller.front;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.mapper.NewsMapper;
import com.example.demo.pojo.News;
import com.example.demo.utils.LayuiResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/front/news")
public class FrontNewsController {


    @Resource
    private NewsMapper newsMapper;


    @RequestMapping("/getAll")
    public Object getAll(){
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        List<News> news = newsMapper.selectList(queryWrapper);
        Integer count = newsMapper.selectCount(queryWrapper);
        return LayuiResult.selectSuccess(news,count);
    }

    @RequestMapping("/getById/{id}")
    public Object getById(@PathVariable Integer id){
        News news = newsMapper.selectById(id);
        Integer count = news.getNewsViewCount();
        news.setNewsViewCount(count + 1);
        newsMapper.updateById(news);
        return LayuiResult.LayuiData(0,"详情信息",news);
    }

}
